import { logout, login } from '@/api/user'
// login,
import {
  getToken,
  setToken,
  removeToken,
  getUserInfo,
  setUserInfo,
  removeUserInfo,
  setInvoiceInfo
} from '@/utils/auth'
import router, { resetRouter } from '@/router'

const state = {
  token: getToken(),
  userInfo: getUserInfo(),
  introduction: '',
  roles: '',
  changePwd: false
}

const mutations = {
  SET_ISOPENSINA: (state, isOpenSina) => {
    state.isOpenSinaPay = isOpenSina
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_USER_INFO: (state, userInfo) => {
    state.userInfo = userInfo
  },
  OPEN_CHANGE_PWD: (state, open) => {
    state.changePwd = open
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      commit('SET_ISOPENSINA', true)
      commit('SET_TOKEN', 'v2:supplier:pc:token:edc94d89ab0a22ed45f6622b76b0921c')
      setToken('v2:supplier:pc:token:edc94d89ab0a22ed45f6622b76b0921c')
      setUserInfo({ name: 'jpp', avatar: require('@/assets/logo.png') })
      resolve({ token: 'v2:supplier:pc:token:edc94d89ab0a22ed45f6622b76b0921c', code: 200, message: 'success' })
      // login({ username: username.trim(), password: password })
      //   .then(({ data = { token: '', user: {}}, code, message }) => {
      //     console.log('data', data)
      //     const user = data && data.user.user
      //     const { token } = data
      //     if (data && token) {
      //       commit('SET_TOKEN', token)
      //       setToken(token)
      //       setInvoiceInfo(data)
      //       if (user) {
      //         commit('SET_USER_INFO', user)
      //         setUserInfo(user)
      //       }
      //     }
      //     resolve({ token, user, code, message })
      //   })
      //   .catch(error => {
      //     reject(error)
      //   })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      const data = {}
      commit('SET_ROLES', ['admin'])
      data.roles = ['admin']
      resolve(data)
    })
    // return new Promise((resolve, reject) => {
    //   getInfo(state.token)
    //     .then(({ data }) => {
    //       if (!data) {
    //         reject('登录失效，请重新登录。')
    //       }
    //       commit('SET_ROLES', data)
    //       // roles must be a non-empty array
    //       if (!data || data.length <= 0) {
    //         reject('该账号当前无任何权限，请联系管理员。')
    //       }
    //       resolve({ roles: data })
    //     })
    //     .catch(error => {
    //       reject(error)
    //     })
    // })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token)
        .then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_USER_INFO', {})
          removeUserInfo()
          removeToken()
          resetRouter()
          resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resolve()
    })
  },

  // dynamically modify permissions
  changeRoles({ commit, dispatch }, role) {
    return new Promise(async resolve => {
      const token = role + '-token'

      commit('SET_TOKEN', token)
      setToken(token)

      const { roles } = await dispatch('getInfo')

      resetRouter()

      // generate accessible routes map based on roles
      const accessRoutes = await dispatch('permission/generateRoutes', roles, {
        root: true
      })

      // dynamically add accessible routes
      router.addRoutes(accessRoutes)

      // reset visited views and cached views
      dispatch('tagsView/delAllViews', null, { root: true })

      resolve()
    })
  },
  toggleChangePwd({ commit }, open) {
    console.log(open)
    commit('OPEN_CHANGE_PWD', open)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
